package net.huashitong.ssydt.provider.exam.service;

import net.huashitong.ssydt.exam.entity.SsExamPaper;
import net.huashitong.ssydt.exam.entity.SsExamPaperQuestion;
import net.huashitong.ssydt.exam.entity.vo.ExamPaperQuestionInVO;
import net.huashitong.ssydt.exam.entity.vo.PaperQuestionOutVO;
import net.huashitong.ssydt.exceptions.ServiceException;
import net.huashitong.ssydt.provider.exam.mapper.SsExamPaperQuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.09.04
 */
@Service
@Transactional
public class SsExamPaperQuestionServiceImpl implements SsExamPaperQuestionService {

    private SsExamPaperQuestionMapper mapper;

    private SsExamPaperService ssExamPaperService;

    private SsExamQuestionService ssExamQuestionService;

    @Autowired
    public SsExamPaperQuestionServiceImpl(SsExamPaperQuestionMapper mapper,
                                          SsExamPaperService ssExamPaperService,
                                          SsExamQuestionService ssExamQuestionService) {
        this.mapper = mapper;
        this.ssExamPaperService = ssExamPaperService;
        this.ssExamQuestionService = ssExamQuestionService;
    }

    @Override
    public List<SsExamPaperQuestion> findByPaperId(Long paperId) {
        return mapper.findByPaperId(paperId);
    }

    @Override
    public List<PaperQuestionOutVO> listPaperQuestionsAndOptions(Long paperId) {
        SsExamPaper ssExamPaper = ssExamPaperService.getById(paperId);
        if (ssExamPaper == null) {
            throw new ServiceException("试卷不存在");
        }
        return mapper.listPaperQuestionsAndOptions(paperId);
    }

    @Override
    public void appendPaperQuestion(Long paperId, ExamPaperQuestionInVO examPaperQuestionInVO) {
        SsExamPaper ssExamPaper = ssExamPaperService.getById(paperId);
        if (ssExamPaper == null) {
            throw new ServiceException("试卷不存在");
        }

        // 统计试卷中题目数量，新增的题目顺序在总数上加 1
        SsExamPaperQuestion ssExamPaperQuestion = new SsExamPaperQuestion();
        ssExamPaperQuestion.setPaperId(paperId);
        int count = mapper.countPaperQuestion(ssExamPaperQuestion);

        for (Long questionId : examPaperQuestionInVO.getQuestions()) {
            if (ssExamQuestionService.getById(questionId) == null) { // 题目不存在
                continue;
            }
            ssExamPaperQuestion.setQuestionId(questionId);
            if (mapper.countPaperQuestion(ssExamPaperQuestion) != 0) { // 试卷题目已存在
                continue;
            }
            ssExamPaperQuestion.setSequence(++count);
            mapper.insert(ssExamPaperQuestion);
        }

        ssExamPaperService.updatePaperQuestionNumber(paperId, count);
    }

    @Override
    public void updatePaperQuestion(Long paperId, ExamPaperQuestionInVO examPaperQuestionInVO) {
        SsExamPaper ssExamPaper = ssExamPaperService.getById(paperId);
        if (ssExamPaper == null) {
            throw new ServiceException("试卷不存在");
        }

        SsExamPaperQuestion ssExamPaperQuestion = new SsExamPaperQuestion();
        ssExamPaperQuestion.setPaperId(paperId);
        List<Long> list = examPaperQuestionInVO.getQuestions();
        int i = 0;
        int size = list.size();
        while (i < size) {
            ssExamPaperQuestion.setQuestionId(list.get(i));
            ssExamPaperQuestion.setSequence(++i);
            mapper.updatePaperQuestionSequence(ssExamPaperQuestion);
        }
    }

    @Override
    public void deletePaperQuestion(Long paperId, ExamPaperQuestionInVO examPaperQuestionInVO) {
        SsExamPaper ssExamPaper = ssExamPaperService.getById(paperId);
        if (ssExamPaper == null) {
            throw new ServiceException("试卷不存在");
        }

        // 删除试卷题目
        SsExamPaperQuestion ssExamPaperQuestion = new SsExamPaperQuestion();
        ssExamPaperQuestion.setPaperId(paperId);
        for (Long questionId : examPaperQuestionInVO.getQuestions()) {
            ssExamPaperQuestion.setQuestionId(questionId);
            mapper.deletePaperQuestion(ssExamPaperQuestion);
        }

        // 更新试卷题目顺序
        List<SsExamPaperQuestion> list = mapper.findByPaperId(paperId);
        int i = 0;
        int size = list.size();
        while (i < size) {
            ssExamPaperQuestion = list.get(i);
            ssExamPaperQuestion.setSequence(++i);
            mapper.updatePaperQuestionSequence(ssExamPaperQuestion);
        }

        ssExamPaperService.updatePaperQuestionNumber(paperId, size);
    }

    @Override
    public int insert(SsExamPaperQuestion record) {
        return mapper.insert(record);
    }

    @Override
    public int batchInsert(List<SsExamPaperQuestion> ssExamPaperQuestionList) {
        return mapper.batchInsert(ssExamPaperQuestionList);
    }
}
